<?php
/**
 * This file is part of the Anota project.
 * 2011-2012 Fernando Garcia Huerta <fgarciahue@uoc.edu>
 * 
 * UOC - FUNDAMENTOS DE INFORMÁTICA EN ENTORNOS BIOINFORMÁTICOS - PEC2
 */

/**
 * AnotaUser
 * 
 * Manages php session and user errors
 * 
 * @package    Anota
 * @subpackage application
 * @author     Fernando Garcia Huerta <fgarciahue@uoc.edu>
 */
class AnotaUser
{

    protected
    $errors = array();  // Last errors

    /**
     * Constructor.
     * - start session
     * - retrieve last setted errors
     * - clean $_SESSION['errors'] variable
     * 
     */
    public function __construct()
    {
        session_start();
        if (isset($_SESSION['errors']))
        {
            $this->errors = $_SESSION['errors'];
            unset($_SESSION['errors']);
        } else
        {
            $this->errors = array();
        }
    }

    /**
     * Sets errors
     * 
     * @param array $errors 
     */
    public function setErrors($errors)
    {
        $_SESSION['errors'] = $errors;
    }
    
    /**
     * Add a single error
     * 
     * @param string $key
     * @param string $error 
     */
    public function addError($key, $error)
    {
        $_SESSION['errors'][$key] = $error;
    }

    /**
     * Returns true if $key error exists
     * 
     * @param string $key
     * @return bool
     */
    public function existError($key)
    {
        return isset($this->errors[$key]);
    }
    
    /**
     * Returns the required error or null
     * 
     * @param string $key
     * @return string or false
     */
    public function getError($key)
    {
        if (isset($this->errors[$key])) {
            return $this->errors[$key];
        }
        else
        {
            return null;
        }
    }
    
    /**
     * Returns last setted errors
     * 
     * @return array()
     */
    public function getErrors()
    {
        return $this->errors;
    }

}
